/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from 'antd';
import isFunction from 'lodash/isFunction';
import routeConfig from '@/utils/routes.config';
import { filterPermissionsCheckKeys } from '@/components/_page/admin/editMenu/authority-extend';

/**
 *
 * @description 获取需要做权限设置的菜单
 * @param routes
 * @returns
 */
export function userAuthorityRoutes(routes: any[]): any[] {
  return routes!
    .filter((item1: any) => !!(item1.name && item1.routes && (item1.showAuth || !item1.hideInMenu)))
    .map((item2: any) => ({
      key: item2.path,
      title: item2.name,
      children: item2.routes
        .filter((item3: any) => !!(item3.name && (item3.showAuth || !item3.hideInMenu)))
        .filter((item5: any) =>
          getRouteArr().find((item6: any) => item6.authority === item5.path.replace('/f/', '')),
        )
        .map((item4: any) => {
          const key = item4.path.replace('/f/', '');
          const keyCode = routeConfig[key];
          return {
            permissions: item4.permissions
              ? item4.permissions.map((item5: { key: string; title: string }) => ({
                  value: `${keyCode}-${item5.key}`,
                  label: item5.title,
                }))
              : null,
            title: item4.name,
            key,
          };
        }),
    }))
    .filter((item5: any) => item5.children.length > 0);
}

export function expandRoutes(routes: any[]) {
  return userAuthorityRoutes(routes)
    .map((item: any) => item.children)
    .reduce((a, b) => a.concat(b));
}

export function getRouteArr() {
  return Object.entries(routeConfig).map((item) => ({ authority: item[0], authKey: item[1] }));
}

export function getAuthKey(authDetailList: any, checkKeys: string[]) {
  return getRouteArr()
    .filter((item: any) => authDetailList.find((item1: any) => item1.key === item.authority))
    .filter((item: any) => !!checkKeys.find((item1: any) => item.authority == item1))
    .map((item3: any) => +item3.authKey);
}

export function getAuthRoute(menu_list: number[]) {
  return getRouteArr()
    .filter((item: any) => !!menu_list.find((item1: any) => item.authKey == item1))
    .map((item2: any) => item2.authority);
}

export function getAuthDetailList(authDetailList: any, authArr: any) {
  return authDetailList
    .filter(
      (item: any) =>
        !!getRouteArr()
          .filter((item1: any) => authArr.includes(item1.authKey))
          .find((item2: any) => item.key === item2.authority),
    )
    .map((item3: any) => {
      const { permissions, title } = item3;
      if (permissions) {
        // 补充permissions权限提醒
        const labelText = permissions
          .filter((item: any) => authArr.includes(item.value))
          .map((item: any) => item.label)
          .join('、');
        if (labelText) {
          return `${title}：${labelText}`;
        }
      }
      return title;
    })
    .join('；');
}

export function callback(params: any, { cb, successMsg }: any) {
  const { code, data, msg } = params;
  if (code == 0) {
    if (successMsg) {
      message.success(msg);
    }
    if (isFunction(cb)) {
      cb(data);
    }
    return new Promise<void>((resolve) => {
      resolve(data);
    });
  } else {
    message.error(msg);
  }
}
